# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# Author     ：skyTree
# version    ：python 3.11
# Description：数据库模型类文件
"""
from tortoise import fields
from tortoise.models import Model


class TimestampMixin(Model):
    create_time = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        abstract = True


class User(TimestampMixin):
    """
    用户模型
    """
    name = fields.CharField(max_length=20, unique=True, description="姓名")
    password = fields.CharField(max_length=522, description="密码")
    nick_name = fields.CharField(max_length=20, null=True, description="昵称")
    phone = fields.CharField(max_length=11, unique=True, null=True, description="手机号")
    email = fields.CharField(max_length=20, unique=True, null=True, description="邮箱")
    roles = fields.ManyToManyField("base.Role", related_name="users", on_delete=fields.CASCADE, description="用户角色")

    class Meta:
        table = "user"
        table_description = "用户表"


class Role(TimestampMixin):
    """
    角色模型
    """
    name = fields.CharField(max_length=32, unique=True, description='角色名称')
    desc = fields.CharField(max_length=128, null=True, description='角色描述')
    users = fields.ManyToManyRelation['User']
    menus = fields.ManyToManyRelation['Menu']

    class Meta:
        table = "role"
        table_description = "角色表"


class Menu(TimestampMixin):
    """
    菜单模型
    """
    name = fields.CharField(max_length=32, unique=True, description='菜单名称')
    level = fields.IntField(null=True, description='菜单等级')
    path = fields.CharField(max_length=32, null=True, description='菜单路径')
    scopes = fields.CharField(max_length=32, null=True, description='菜单权限标识')
    is_menu = fields.BooleanField(default=True, description='是否为菜单')
    pid = fields.ForeignKeyField("base.Menu", related_name="children", null=True, on_delete=fields.CASCADE,
                                 description='父级菜单')
    children = fields.ReverseRelation['Menu']

    roles = fields.ManyToManyField("base.Role", related_name="menus", on_delete=fields.CASCADE, description="角色菜单")

    class Meta:
        table = "menu"
        table_description = "菜单表"


class Category(TimestampMixin):
    """
    商品分类模型
    """
    name = fields.CharField(max_length=32, unique=True, description='分类名称')
    level = fields.IntField(default=1, comment='分类等级')
    pid = fields.ForeignKeyField("base.Category", related_name="children", null=True, on_delete=fields.CASCADE,
                                 description='父级分类')
    children = fields.ReverseRelation['Category']
    attrs = fields.ReverseRelation['Attribute']

    good = fields.ReverseRelation['Goods']

    class Meta:
        table = "category"
        table_description = "商品分类表"


class Attribute(TimestampMixin):
    name = fields.CharField(max_length=32, unique=True, description='属性名称')
    val = fields.CharField(max_length=255, null=True, description='属性值')
    _type = fields.CharField(max_length=32, default='static', description='属性类型,static:静态；dynamic:动态')
    cid = fields.ForeignKeyField("base.Category", related_name="attrs", null=True, on_delete=fields.CASCADE,
                                 description='所属分类')

    class Meta:
        table = "attr"
        table_description = "商品属性表"


class Goods(TimestampMixin):
    name = fields.CharField(max_length=512, unique=True, description='商品名称')
    price = fields.FloatField(null=True, description='商品价格')
    number = fields.IntField(null=True, description='商品数量')
    introduce = fields.TextField(null=True, description='商品介绍')
    big_log = fields.CharField(max_length=512, null=True, description='商品大图')
    small_log = fields.CharField(max_length=512, null=True, description='商品小图')
    state = fields.IntField(default=0, description='商品状态，0未通过 1审核中 2已审核')  # 0未通过 1审核中 2已审核
    is_promote = fields.IntField(default=0, description='是否畅销，0否 1是')  # 0否 1是
    hot_number = fields.IntField(null=True, description='热卖数量')
    weight = fields.IntField(null=True, description='商品重量')

    cid_one = fields.ForeignKeyField("base.Category", related_name="related_cid_one", null=True,
                                     on_delete=fields.CASCADE, description='所属一级分类id')
    cid_two = fields.ForeignKeyField("base.Category", related_name="related_cid_two", null=True,
                                     on_delete=fields.CASCADE, description='所属二级分类id')
    cid_three = fields.ForeignKeyField("base.Category", related_name="related_cid_three", null=True,
                                       on_delete=fields.CASCADE, description='所属三级分类id')

    static_attrs = fields.ManyToManyField("base.Attribute", related_name="good_static", on_delete=fields.CASCADE,
                                          description="静态属性值")
    dynamic_attrs = fields.ManyToManyField("base.Attribute", related_name="good_dynamic", on_delete=fields.CASCADE,
                                           description="动态属性值")

    class Meta:
        table = "good"
        table_description = "商品表"
